package com.ksd.modules.download.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.download.entity.AdminDownloads;
import com.ksd.modules.download.mapper.AdminDownloadsMapper;
import com.ksd.modules.download.po.AdminDownloadsSavePO;
import com.ksd.modules.download.po.AdminDownloadsSearchPO;
import com.ksd.modules.download.service.IAdminDownloadsService;
import com.ksd.modules.download.vo.AdminDownloadsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminDownloadsServiceImpl extends BaseServiceImpl<AdminDownloadsMapper, AdminDownloads> implements IAdminDownloadsService {

    /**
     * 查询列表
     * @param adminDownloadsSearchPO
     * @return
     */
    public Page<AdminDownloadsVo> findAdminDownloadss(AdminDownloadsSearchPO adminDownloadsSearchPO){
        Page<AdminDownloads> page = new Page<>(adminDownloadsSearchPO.getPageNo(),adminDownloadsSearchPO.getPageSize());
        LambdaQueryWrapper<AdminDownloads> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(AdminDownloads.class, c -> !c.getColumn().equalsIgnoreCase("content"));
        lambdaQueryWrapper.eq(AdminDownloads::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminDownloads::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getStatus()), AdminDownloads::getStatus,adminDownloadsSearchPO.getStatus());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getDownloadstype()), AdminDownloads::getDownloadstype,adminDownloadsSearchPO.getDownloadstype());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getNewFlag()), AdminDownloads::getNewFlag,adminDownloadsSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getHotFlag()), AdminDownloads::getHotFlag,adminDownloadsSearchPO.getHotFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getPushFlag()), AdminDownloads::getPushFlag,adminDownloadsSearchPO.getPushFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCategoryId()), AdminDownloads::getCategoryId,adminDownloadsSearchPO.getCategoryId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCategoryPid()), AdminDownloads::getCategoryPid,adminDownloadsSearchPO.getCategoryPid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCommentFlag()), AdminDownloads::getCommentFlag,adminDownloadsSearchPO.getCommentFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getMode()), AdminDownloads::getMode,adminDownloadsSearchPO.getMode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getVipFlag()), AdminDownloads::getVipFlag,adminDownloadsSearchPO.getVipFlag());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminDownloadsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminDownloads::getTitle,adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getNickname, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getUuid, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getTags, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getCategoryPname, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getCategoryName, adminDownloadsSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminDownloads::getCreateTime);
        Page<AdminDownloads> adminDownloadsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminDownloadsVo> adminDownloadsVoPage = tranferPageBo(adminDownloadsPage, AdminDownloadsVo.class);
        return adminDownloadsVoPage;
    }

    /**
     * 回收站
     * @param adminDownloadsSearchPO
     * @return
     */
    public Page<AdminDownloadsVo> findAdminDownloadssRecyle(AdminDownloadsSearchPO adminDownloadsSearchPO){
        Page<AdminDownloads> page = new Page<>(adminDownloadsSearchPO.getPageNo(),adminDownloadsSearchPO.getPageSize());
        LambdaQueryWrapper<AdminDownloads> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(AdminDownloads.class, c -> !c.getColumn().equalsIgnoreCase("content"));
        lambdaQueryWrapper.eq(AdminDownloads::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminDownloads::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getStatus()), AdminDownloads::getStatus,adminDownloadsSearchPO.getStatus());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getDownloadstype()), AdminDownloads::getDownloadstype,adminDownloadsSearchPO.getDownloadstype());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getNewFlag()), AdminDownloads::getNewFlag,adminDownloadsSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getHotFlag()), AdminDownloads::getHotFlag,adminDownloadsSearchPO.getHotFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getPushFlag()), AdminDownloads::getPushFlag,adminDownloadsSearchPO.getPushFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCategoryPid()), AdminDownloads::getCategoryPid,adminDownloadsSearchPO.getCategoryPid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCategoryId()), AdminDownloads::getCategoryId,adminDownloadsSearchPO.getCategoryId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getCommentFlag()), AdminDownloads::getCommentFlag,adminDownloadsSearchPO.getCommentFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getMode()), AdminDownloads::getMode,adminDownloadsSearchPO.getMode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsSearchPO.getVipFlag()), AdminDownloads::getVipFlag,adminDownloadsSearchPO.getVipFlag());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminDownloadsSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminDownloads::getTitle,adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getNickname, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getUuid, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getTags, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getCategoryPname, adminDownloadsSearchPO.getKeyword())
                    .or().like(AdminDownloads::getCategoryName, adminDownloadsSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminDownloads::getCreateTime);
        Page<AdminDownloads> adminDownloadsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminDownloadsVo> adminDownloadsVoPage = tranferPageBo(adminDownloadsPage, AdminDownloadsVo.class);
        return adminDownloadsVoPage;
    }

    /**
     * 保存和更新
     * @param adminDownloadsSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminDownloadsVo saveUpdateAdminDownloads(AdminDownloadsSavePO adminDownloadsSavePO){
        AdminDownloads adminDownloads = tranferBo(adminDownloadsSavePO, AdminDownloads.class);
        adminDownloads.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminDownloads.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminDownloads);
        AdminDownloadsVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsVo.class);
        return adminDownloadsVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminDownloadsVo getAdminDownloadsById(Long id){
        AdminDownloads adminDownloads = this.getById(id);
        AdminDownloadsVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsVo.class);
        return adminDownloadsVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminDownloadsById(Long id){
        boolean b =  this.updateStatus(id, AdminDownloads.Columns.IS_DELETED,1);
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminDownloads(Long id){
        boolean b =  this.updateStatus(id, AdminDownloads.Columns.IS_DELETED,0);
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminDownloadsById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminDownloads(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloads> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminDownloads(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloads> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminDownloads(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloads> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloads> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminDownloads data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
